package edu.whu.demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.whu.demo.domain.ActivityDTO;
import edu.whu.demo.domain.Entry;
import edu.whu.demo.domain.User;
import edu.whu.demo.exception.ActivityMemberException;
import edu.whu.demo.exception.EntryException;
import edu.whu.demo.service.IEntryService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/entry")
public class EntryController {

    @Autowired
    IEntryService entryService;

    @ApiOperation("根据活动id查询用户")
    @GetMapping("/{activityId}")
    public IPage<User> getMembers(@ApiParam("活动id")@PathVariable Long activityId,
                                  @ApiParam("用户名关键词")String keyword,
                                  @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                  @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize)
    {
        return entryService.getMembers(activityId, keyword, pageNum, pageSize);
    }

    @ApiOperation("查询用户参与的活动")
    @GetMapping("")
    public IPage<ActivityDTO> getActivityByEntry(@ApiParam("用户id")Long uid,
                                                 @ApiParam("活动类型")String type,
                                                 @ApiParam("排序列")String orderBy,
                                                 @ApiParam("正序/倒序")String order,
                                                 @ApiParam("关键词")String keyword,
                                                 @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                                 @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize)
    {
        Map<String,Object> condition=new HashMap<>();
        if(uid!=null) {
            condition.put("uid", uid);
        }
        if(type!=null) {
            condition.put("type", type);
        }
        if(orderBy!=null) {
            condition.put("orderBy", orderBy);
        }
        if(order!=null) {
            condition.put("order", order);
        }
        if(keyword!=null) {
            condition.put("keyword", keyword);
        }
        return entryService.getActivityByEntry(condition, pageNum, pageSize);

    }

    @ApiOperation("取消参与活动")
    @DeleteMapping("")
    public void delEntry(@RequestBody Entry entry) throws EntryException, ActivityMemberException
    {
        entryService.delEntry(entry);
    }


}

